package com.zhongyi.http;


import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import com.zhongyi.entity.Company;
import com.zhongyi.entity.Contact;
import com.zhongyi.http.base.BaseAction;
import com.zhongyi.http.service.MessageService;
import com.zhongyi.util.Constant;
import com.zhongyi.util.HttpUtil;
import com.zhongyi.util.UUIDGenerator;

/**
 * 企业信息录入处理类
 * <p>
 * 
 *
 * @author <a href="mailto:lysongfei@gmail.com">songfei</a>
 * @version 3.0, 2012-4-24
 */
public class MessageAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String corpName;
	/* text类型ajax返回值  */
//	private String responseText;
	
	private MessageService messageService;
	
	//信息分隔符
	private final String MESSAGE_SEPARATOR = "\n{1,}?";
	
	
	public String getCorpName() {
		return corpName;
	}

	public void setCorpName(String corpName) {
		this.corpName = corpName;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	/**
	 * 处理企业信息录入
	 */
	public void doInput(){
		String resText = "no";
		HttpServletRequest request = HttpUtil.getRequest();
		String uuid = UUIDGenerator.create();
		Company corp = new Company();
		Contact contact = null;
		corp.setId(uuid);
		HttpUtil.setComapny(corp, request);
		corp.setCreate_time(new Timestamp(System.currentTimeMillis()));
		messageService.addCompany(Constant.CORP_INSERT_SQL, corp);
		//处理EMAIL
		String[] values = null;
		String info = corp.getEmail();
		if(info != null && info.length() > 0){
			values = info.split(MESSAGE_SEPARATOR);
			for(String email : values){
				if(email.trim().length() > 0){
					contact = new Contact();
					contact.setId(UUIDGenerator.create());
					contact.setCorp_id(uuid);
					contact.setContact_info(email.trim());
					contact.setInfo_type("E");
					contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
					messageService.addContact(Constant.CONTACT_INSERT_SQL, contact);
				}
			}
		}
		//处理MOBILE
		info = corp.getMobile();
		if(info != null && info.length() > 0){
			values = info.split(MESSAGE_SEPARATOR);
			for(String mobile : values){
				if(mobile.trim().length() > 0){
					contact = new Contact();
					contact.setId(UUIDGenerator.create());
					contact.setCorp_id(uuid);
					contact.setContact_info(mobile.trim());
					contact.setInfo_type("M");
					contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
					messageService.addContact(Constant.CONTACT_INSERT_SQL, contact);
				}
			}
		}
		//处理PHONE
		info = corp.getPhone();
		if(info != null && info.length() > 0){
			values = info.split(MESSAGE_SEPARATOR);
			for(String phone : values){
				if(phone.trim().length() > 0){
					contact = new Contact();
					contact.setId(UUIDGenerator.create());
					contact.setCorp_id(uuid);
					contact.setContact_info(phone.trim());
					contact.setInfo_type("P");
					contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
					messageService.addContact(Constant.CONTACT_INSERT_SQL, contact);
				}
			}
		}
		//处理FAX
		info = corp.getFax();
		if(info != null && info.length() > 0){
			values = info.split(MESSAGE_SEPARATOR);
			for(String fax : values){
				if(fax.trim().length() > 0){
					contact = new Contact();
					contact.setId(UUIDGenerator.create());
					contact.setCorp_id(uuid);
					contact.setContact_info(fax.trim());
					contact.setInfo_type("F");
					contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
					messageService.addContact(Constant.CONTACT_INSERT_SQL, contact);
				}
			}
		}
		try {
			resText = "yes";
			HttpUtil.getResponse().getWriter().write(resText);
			HttpUtil.getResponse().getWriter().flush();
			HttpUtil.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String doCorpUniqueValidate(){
		String responseText = "true";
		if(this.corpName != null && this.corpName.trim().length() > 0){
			int count = messageService.getComanyCount(Constant.CORP_COUNT_SQL, this.corpName);
			if(count > 0){
				responseText = "false";
			}
		}
		try {
			HttpUtil.getResponse().getWriter().write(responseText);
			HttpUtil.getResponse().getWriter().flush();
			HttpUtil.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return NONE;
	}
	
}
